#include "edit_node_dialog.h"
#include "ui_edit_node_dialog.h"
#include <QMessageBox>

EditNodeDialog::EditNodeDialog(StateInfo *state_info, QWidget *parent) :
    QDialog(parent),
    ui_(new Ui::EditNodeDialog),
    state_info_(state_info)
{
    ui_->setupUi(this);
    
    // 设置窗口标题
    setWindowTitle("编辑节点");
    
    // 初始化界面数据
    if (state_info_) {
        ui_->nameEdit->setText(state_info_->stateName());
        ui_->hostEdit->setText(state_info_->host());
        ui_->portEdit->setValue(state_info_->port());
    }
    if (state_info_->parentId() == 0) {
        ui_->portEdit->setDisabled(true);
    }
    
    // 连接信号槽
    connect(ui_->buttonBox, &QDialogButtonBox::accepted, this, &EditNodeDialog::onAccept);
    connect(ui_->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
}

EditNodeDialog::~EditNodeDialog()
{
    delete ui_;
}

void EditNodeDialog::onAccept()
{
    // 获取编辑后的数据
    QString name = ui_->nameEdit->text().trimmed();
    QString host = ui_->hostEdit->text().trimmed();
    int port = ui_->portEdit->value();
    
    // 数据验证
    if (name.isEmpty()) {
        QMessageBox::warning(this, "警告", "名称不能为空");
        return;
    }
    
    if (host.isEmpty()) {
        QMessageBox::warning(this, "警告", "主机地址不能为空");
        return;
    }
    
    // 直接修改传入的状态信息对象
    state_info_->setStateName(name);
    state_info_->setHost(host);
    state_info_->setPort(port);
    
    // 关闭对话框
    accept();
} 